package com.woniuxy.user.controller;


import com.woniuxy.entity.ResponseResult;
import com.woniuxy.user.entity.User;
import com.woniuxy.user.jwt.Audience;
import com.woniuxy.user.jwt.JwtUtil;
import com.woniuxy.user.service.UserService;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wf
 * @since 2021-03-26
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private Audience audience;

    /**
     * 登录服务
     * @param account
     * @return
     */
    @PostMapping("accountAndPassword")
    public Map<String,String> accountAndPassword(@RequestBody String account){
        Map<String,String> map = userService.accountAndPassword(account);
        return map;
    }

    /**
     * 修改用户头像
     * @param file
     * @return
     */
    @PostMapping("changIcon")
    public ResponseResult<?> changIcon(MultipartFile file, HttpServletRequest request){
        String account = JwtUtil.getAccount(request.getHeader("Authorization"),audience.getBase64Secret());
        ResponseResult<?> responseResult = new ResponseResult<>();
        if (userService.changIcon(account,file)){
            responseResult.setCode(200);
            responseResult.setMsg("success!");
        }else {
            responseResult.setCode(601);
            responseResult.setMsg("失败!请稍后重试！");
        }
        return responseResult;
    }

    /**
     * 修改用户密码
     * @param
     * @return
     */
    @PostMapping("/changPassword")
    public ResponseResult<?> changPassword(@RequestBody User user, HttpServletRequest request){
        String account = JwtUtil.getAccount(request.getHeader("Authorization"),audience.getBase64Secret());
        ResponseResult<?> responseResult = new ResponseResult<>();
        if (userService.changPassword(account,user.getPassword())){
            responseResult.setCode(200);
            responseResult.setMsg("success!");
        }else {
            responseResult.setCode(601);
            responseResult.setMsg("失败!请稍后重试！");
        }
        return responseResult;
    }


}

